package src.main.java.com.pms.controller;

import src.main.java.com.pms.service.*;
import src.main.java.com.pms.util.InputUtil;

import java.sql.SQLException;

/**
 * 控制台Controller 以弃用
 */

public class PetController {
    private final PetBasicService petBasicService;
    private final PetStatisticsService petStatisticsService;
    private final AppointmentService appointmentService;
    private final SaleService saleService;
    private final UserService userService;
    private String currentUser = null;

    public PetController() {
        this.petBasicService = new PetBasicService();
        this.petStatisticsService = new PetStatisticsService();
        this.appointmentService = new AppointmentService();
        this.saleService = new SaleService();
        this.userService = new UserService();
    }

    public void start() {
        while (true) {
            if (currentUser == null) {
                showLoginMenu();
            } else {
                showMainMenu();
            }
        }
    }

    private void showLoginMenu() {
        System.out.println("\n=== 宠物管理系统 - 登录/注册 ===");
        System.out.println("1. 登录");
        System.out.println("2. 注册");
        System.out.println("3. 退出系统");

        int choice = InputUtil.getInt("请选择操作: ");
        switch (choice) {
            case 1 -> login();
            case 2 -> register();
            case 3 -> {
                System.out.println("感谢使用，再见！");
                System.exit(0);
            }
            default -> System.out.println("无效的选择，请重试。");
        }
    }

    private void login() {
        try {
            String username = InputUtil.getString("请输入用户名: ", 3, 20);
            String password = InputUtil.getString("请输入密码: ", 6, 20);

            if (userService.login(username, password)) {
                currentUser = username;
                System.out.println("登录成功！欢迎回来，" + username);
            }
        } catch (RuntimeException e) {
            System.out.println("登录失败: " + e.getMessage());
        }
    }

    private void register() {
        try {
            String username = InputUtil.getString("请输入用户名(3-20个字符): ", 3, 20);
            String password = InputUtil.getString("请输入密码(6-20个字符): ", 6, 20);
            String confirmPassword = InputUtil.getString("请确认密码: ", 6, 20);

            if (!password.equals(confirmPassword)) {
                System.out.println("两次输入的密码不一致！");
                return;
            }

            if (userService.register(username, password)) {
                System.out.println("注册成功！请登录。");
            }
        } catch (RuntimeException e) {
            System.out.println("注册失败: " + e.getMessage());
        }
    }

    private void showMainMenu() {
        try {
            System.out.println("\n=== 宠物管理系统主菜单 ===");
            System.out.println("1. 基础宠物管理");
            System.out.println("2. 宠物统计功能");
            System.out.println("3. 预约管理");
            System.out.println("4. 寄卖管理");
            System.out.println("5. 退出登录");
            System.out.println("6. 退出系统");

            int choice = InputUtil.getInt("请选择操作(1-6): ", 1, 6);
            handleMainMenuChoice(choice);
        } catch (RuntimeException e) {
            System.out.println("操作错误: " + e.getMessage());
            System.out.println("返回主菜单...");
        }
    }
    private void handleMainMenuChoice(int choice) {
        try {
            switch (choice) {
                case 1 -> showBasicManagementMenu();
                case 2 -> showStatisticsMenu();
                case 3 -> showAppointmentMenu();
                case 4 -> showSaleMenu();
                case 5 -> {
                    currentUser = null;
                    System.out.println("已退出登录");
                }
                case 6 -> {
                    System.out.println("感谢使用，再见！");
                    System.exit(0);
                }
            }
        } catch (Exception e) {
            System.out.println("操作过程中发生错误: " + e.getMessage());
        }
    }

    private void showBasicManagementMenu() throws SQLException, ClassNotFoundException {
        while (true) {
            System.out.println("\n=== 基础宠物管理 ===");
            System.out.println("1. 添加宠物");
            System.out.println("2. 查看所有宠物");
            System.out.println("3. 更新宠物信息");
            System.out.println("4. 删除宠物");
            System.out.println("5. 按名字搜索宠物");
            System.out.println("6. 返回主菜单");

            int choice = InputUtil.getInt("请选择操作: ");
            switch (choice) {
                case 1 -> petBasicService.addPet();
                case 2 -> petBasicService.viewPets();
                case 3 -> petBasicService.updatePet();
                case 4 -> petBasicService.deletePet();
                case 5 -> petBasicService.searchPetByName();
                case 6 -> {
                    return;
                }
                default -> System.out.println("无效的选择，请重试。");
            }
        }
    }

    private void showStatisticsMenu() {
        while (true) {
            System.out.println("\n=== 宠物统计功能 ===");
            System.out.println("1. 计算平均年龄");
            System.out.println("2. 按类型统计数量");
            System.out.println("3. 查看最老宠物");
            System.out.println("4. 统计年龄段分布");
            System.out.println("5. 返回主菜单");

            int choice = InputUtil.getInt("请选择操作: ");
            switch (choice) {
                case 1 -> petStatisticsService.calculateAverageAge();
                case 2 -> petStatisticsService.countPetsByType();
                case 3 -> petStatisticsService.viewOldestPet();
                case 4 -> petStatisticsService.countPetsByAgeRange();
                case 5 -> {
                    return;
                }
                default -> System.out.println("无效的选择，请重试。");
            }
        }
    }

    private void showAppointmentMenu() {
        while (true) {
            System.out.println("\n=== 预约管理 ===");
            System.out.println("1. 添加洗澡预约");
            System.out.println("2. 添加疫苗预约");
            System.out.println("3. 添加火化预约");
            System.out.println("4. 查看所有预约");
            System.out.println("5. 取消洗澡预约");
            System.out.println("6. 取消疫苗预约");
            System.out.println("7. 取消火化预约");
            System.out.println("8. 完成洗澡预约");
            System.out.println("9. 完成疫苗预约");
            System.out.println("10. 完成火化预约");
            System.out.println("11. 返回主菜单");

            int choice = InputUtil.getInt("请选择操作: ");
            switch (choice) {
                case 1 -> appointmentService.addBathAppointment();
                case 2 -> appointmentService.addVaccineAppointment();
                case 3 -> appointmentService.addCremationAppointment();
                case 4 -> appointmentService.viewAllAppointments();
                case 5 -> appointmentService.cancelAppointment("bath");
                case 6 -> appointmentService.cancelAppointment("vaccine");
                case 7 -> appointmentService.cancelAppointment("cremation");
                case 8 -> appointmentService.completeAppointment("bath");
                case 9 -> appointmentService.completeAppointment("vaccine");
                case 10 -> appointmentService.completeAppointment("cremation");
                case 11 -> {
                    return;
                }
                default -> System.out.println("无效的选择，请重试。");
            }
        }
    }

    private void showSaleMenu() {
        while (true) {
            System.out.println("\n=== 寄卖管理 ===");
            System.out.println("1. 设置宠物寄卖");
            System.out.println("2. 取消宠物寄卖");
            System.out.println("3. 更新寄卖价格");
            System.out.println("4. 查看所有寄卖宠物");
            System.out.println("5. 返回主菜单");

            int choice = InputUtil.getInt("请选择操作: ");
            switch (choice) {
                case 1 -> saleService.setPetForSale(1, 100, null);
                case 2 -> saleService.cancelPetSale(1);
                case 3 -> saleService.updateSalePrice(1, 200);
                case 4 -> saleService.viewPetsForSale();
                case 5 -> {
                    return;
                }
                default -> System.out.println("无效的选择，请重试。");
            }
        }
    }
}